package flinty.autocomplete.combobox.test;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.GroupLayout.Alignment;

abstract class BaseTestFrame extends JFrame {

	private static final long serialVersionUID = 6340274074879202764L;

	private final JComboBox comboBox = new JComboBox();

	private final JTextField textField = new JTextField();

	BaseTestFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		textField.setText("100-200,1100-1200");
		JLabel modelLabel = new JLabel("Model:");
		JButton closeButton = new JButton("Close");
		closeButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				BaseTestFrame.this.setVisible(false);
				System.exit(0);
			}
		});
		JButton decorateButton = new JButton("Decorate");
		decorateButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				String text = textField.getText();
				try {
					LinkedList<Integer> lst = new LinkedList<Integer>();
					for (String str : text.split(",")) {
						int idx = str.indexOf("-");
						if (idx != -1) {
							for (int n = Integer
									.parseInt(str.substring(0, idx)); n <= Integer
									.parseInt(str.substring(idx + 1)); n++) {
								lst.add(Integer.valueOf(n));
							}
							continue;
						}
					}
					decorateComboBox(comboBox, lst);
				} catch (Exception exc) {
					JOptionPane.showMessageDialog(BaseTestFrame.this, "error: "
							+ exc.getMessage());
				}
			}
		});
		GroupLayout layout = new GroupLayout(getContentPane());
		layout.setAutoCreateContainerGaps(true);
		layout.setAutoCreateGaps(true);
		setLayout(layout);
		layout.setHorizontalGroup(layout.createParallelGroup(Alignment.LEADING)//
				.addGroup(
						layout.createSequentialGroup()//
								.addGroup(
										layout.createParallelGroup(
												Alignment.TRAILING)//
												.addComponent(modelLabel)//
												.addComponent(decorateButton)//
								)//
								.addGroup(
										layout.createParallelGroup(
												Alignment.LEADING)//
												.addComponent(textField, 200,
														200, 200)//
												.addComponent(comboBox, 200,
														200, 200)//
								)//
				)//
				.addGroup(layout.createSequentialGroup()//
						.addGap(0, 10, Short.MAX_VALUE)//
						.addComponent(closeButton))//
				);
		layout.setVerticalGroup(layout.createSequentialGroup()//
				.addGroup(layout.createParallelGroup(Alignment.BASELINE)//
						.addComponent(modelLabel)//
						.addComponent(textField)//
				).addGroup(layout.createParallelGroup(Alignment.BASELINE)//
						.addComponent(decorateButton)//
						.addComponent(comboBox)//
				).addGap(0, 10, Short.MAX_VALUE)//
				.addComponent(closeButton));
		pack();
		setResizable(false);
		setLocationRelativeTo(null);
	}

	protected abstract void decorateComboBox(JComboBox combo,
			LinkedList<Integer> lst);

}
